package com.pbase.data.http;

import android.content.Context;

import com.blankj.utilcode.util.Utils;
import com.pbase.data.http.interceptor.CacheInterceptor;
import com.pbase.data.http.interceptor.HeadInterceptor;
import com.pbase.data.http.interceptor.HttpsUtils;
import com.pbase.data.http.interceptor.ProgressInterceptor;
import com.pbase.data.http.interceptor.StringConverterFactory;
import com.pbase.data.http.interceptor.logging.Level;
import com.pbase.data.http.interceptor.logging.LoggingInterceptor;
import com.pbase.data.http.listener.OnDataRequestListenerWithProgress;

import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.ConnectionPool;
import okhttp3.CookieJar;
import okhttp3.internal.platform.Platform;
import retrofit2.Converter;

/**
 * Retrofit构建所需的配置
 */
public class RetrofitBuilderTool {
    private Context mContext = Utils.getApp();

    // ====================================================================
    // okhtp
    // ====================================================================

    /**
     * Cookie保存器
     * 例：new CookieJarImpl(new PersistentCookieStore(mContext))
     */
    public CookieJar buildCookieJar() {
        return null;
    }

    /**
     * 缓存相关
     * 例     File httpCacheDirectory = new File(mContext.getCacheDir(), "goldze_cache");
     *         Cache cache = new Cache(httpCacheDirectory, cacheSpace());
     */
    public Cache buildCache() {

        return null;
    }

    /**
     * 配置缓存空间大小
     * 例 10 * 1024 * 1024
     */
    public int cacheSpace() {
        return 0;
    }

    /**
     * 构造缓存截器
     * 例 new CacheInterceptor(Utils.getApp())
     */
    public CacheInterceptor buildCacheInterceptor() {
        return null;
    }

    /**
     * 构造请求头拦截器
     * 例 new HeadInterceptor(null)
     */
    public HeadInterceptor buildHeadInterceptor() {
        return null;
    }

    /**
     * 配置ssl
     * 例 HttpsUtils.getSslSocketFactory()
     */
    public HttpsUtils.SSLParams buildSSLParams() {
        return null;
    }

    /**
     * 连接超时 TimeUnit.SECONDS
     */
    public int connectTimeout() {
        return 30;
    }

    /**
     * 请求超时 TimeUnit.SECONDS
     */
    public int writeTimeout() {
        return 30;
    }

    /**
     * 读取超时 TimeUnit.SECONDS
     */
    public int readTimeout() {
        return 30;
    }

    /**
     * 连接池配置
     * maxIdleConnections 同时连接个数
     * keepAliveDuration  连接保持时间
     */
    public ConnectionPool buildConnectionPool() {
        return new ConnectionPool(8, 15, TimeUnit.SECONDS);
    }

    /**
     * 进度条拦截器
     * 只有DownlaadManager 中有调用
     * 例 new ProgressInterceptor(listener)
     */
    public  ProgressInterceptor buildProgressInterceptor(OnDataRequestListenerWithProgress listener) {
        return null;
    }

    /**
     * 返回值解析器
     * 可更换为 new GsonConverterFactory()
     */
    public Converter.Factory  buildConverterFactory(){
        return new StringConverterFactory();
    }

    // ====================================================================
    // 日志拦截
    // ====================================================================

    /**
     * 构造日志拦截
     */
    public LoggingInterceptor buildLoggingInterceptor() {
        return new LoggingInterceptor
                .Builder()//构建者模式
                .loggable(islog()) //是否开启日志打印
                .setLevel(Level.BASIC) //打印的等级
                .log(Platform.INFO) // 打印类型
                .request("Request") // request的Tag
                .response("Response")// Response的Tag
                .build();
    }

    /**
     * 日志控制
     */
    public boolean islog() {
        return true;
    }


    // ====================================================================
    // Retrofit
    // ====================================================================
    public String baseUrl() {
        return "https://www.baidu.com";
    }




}
